<?php

namespace App\Admin\Actions;

use App\Library\Logic\AmountLogic;
use App\Library\Logic\OperateLogLogic;
use App\Library\Logic\ShopProductLogic;
use App\Library\Logic\ShopProductSpecsLogic;
use App\Models\AdjustmentModel;
use App\Models\AmountModel;
use App\Models\ImportProductModel;
use Encore\Admin\Actions\Response;
use Encore\Admin\Actions\RowAction;
use Encore\Admin\Facades\Admin;
use Exception;
use Illuminate\Http\Request;

class ImportProductAddAction extends RowAction
{
    public $name = '添加';

    /**
     * handle
     * @param ImportProductModel $model
     * @return Response
     * @throws Exception
     */
    public function handle(ImportProductModel $model): Response
    {
        $ids = [];
        foreach ($model->extend as $item) {
            $shopProduct = ShopProductLogic::insertForModel([
                'productAvatar' => __('com.defaultImg'),
                'mainImgIds' => [__('com.defaultImg')],
                'productGene' => $item['gene'],
                'productName' => $item['name'],
                'productNote' => $item['note'],
                'productLabel' => $item['label'],
                'productStage' => $item['stage'],
                'productContent' => $item['content'],
                'productProtect' => $item['protect'],
                'productActivity' => $item['activity'],
                'productWildCare' => $item['wildCare'],
                'productActivityType' => $item['activityType'],
                'productCategoryId_1' => 1,
                'productCategoryId_2' => 3,
                'productCategoryId_3' => 7,
                'userId' => Admin::user()->user_id,
                'addUserId' => Admin::user()->user_id,
                'editUserId' => Admin::user()->user_id,
                'createdTime' => now()->toDateString(),
                'createdAt' => now()->timestamp,
                'updatedAt' => now()->timestamp,
            ]);
            $spec = ShopProductSpecsLogic::insertForModel([
                'productId' => $shopProduct->product_id,
                'psName' => $item['psName'],
                'psBuyLimit' => $item['psBuyLimit'],
                'psTotalStock' => $item['psUsableStock'],
                'psUsableStock' => $item['psUsableStock'],
                'psPriceUnderlined' => $item['psPriceUnderlined'],
                'userId' => Admin::user()->user_id,
                'createdAt' => now()->timestamp,
                'updatedAt' => now()->timestamp,
            ]);
            cache(['shopProductSpec:' . $spec->serial_number => $spec->ps_id]);
            $shopProduct->ps_id_json = json_encode([$spec->product_id]);
            $shopProduct->spec_json = [$spec->serial_number];
            $shopProduct->save();
            $ids[] = $spec->product_id;
            mq('shopProductChange', $shopProduct);
        }
        $model->product_ids = $ids;
        $model->save();
        return $this->response()->success('操作成功')->refresh();
    }

    /**
     * dialog
     * @return void
     */
    public function dialog(): void
    {
        $this->confirm('确定添加吗?');
    }
}
